package com.example.liaotian.Fragment.MyContent;


import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.liaotian.Adapter.RecycAdapter;
import com.example.liaotian.Adapter.TraAdapter;
import com.example.liaotian.Bean.Act;
import com.example.liaotian.Bean.Post;
import com.example.liaotian.Bean.Tra;
import com.example.liaotian.Bean.User;
import com.example.liaotian.R;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyTraFragment extends Fragment {
    private SwipeMenuRecyclerView recyclerView;
    private List<Tra> data;//存放帖子的集合
    private TraAdapter recycAdapter;


    public MyTraFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my_tra, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        Refresh();//fragment加载页面的时候就刷新一次
    }

    private void Refresh() {

        User user = BmobUser.getCurrentUser(User.class);//创建对象
        BmobQuery<Tra> query = new BmobQuery<>();//
        query.addWhereEqualTo("author",user);
        query.order("-createdAt");
        query.findObjects(new FindListener<Tra>() {
            @Override
            public void done(List<Tra> list, BmobException e) {

                if (e == null){
                    data = list;
                    if (data.size()>0){

                        recyclerView.addItemDecoration(new DefaultItemDecoration(Color.GRAY));
                        recyclerView.setSwipeMenuItemClickListener(swipeMenuItemClickListener);
                        recyclerView.setSwipeMenuCreator(swipeMenuCreator);

                        recycAdapter = new TraAdapter(getActivity(),data);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        recyclerView.setAdapter(recycAdapter);
                    }else {

                    }
                }else {
                    Toast.makeText(getActivity(),"加载失败！",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    /**
     * 设置右滑菜单
     */
    SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.dp_70);//宽度70
            int height = ViewGroup.MarginLayoutParams.MATCH_PARENT;//高度与父组件一样
            /**
             * 设置颜色大小并填入
             */
            SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                    .setBackgroundColor(Color.RED)
                    .setTextColor(Color.WHITE)
                    .setText("删除")
                    .setWidth(width)
                    .setHeight(height);
            //填入
            swipeRightMenu.addMenuItem(deleteItem);

        }
    };
    /**
     * 右滑菜单的点击事件
     */
    SwipeMenuItemClickListener swipeMenuItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            //任何操作都会关闭菜单
            menuBridge.closeMenu();

            int dirrection = menuBridge.getDirection();//左边还是右边菜单
            final int adapterPosition = menuBridge.getAdapterPosition();//recyclerview的item的位置
            int position = menuBridge.getPosition();//菜单在recyclerview的位置
            //如果相等，就删除当前位置的item
            if (dirrection == SwipeMenuRecyclerView.RIGHT_DIRECTION){

                Tra post = new Tra();
                post.setObjectId(data.get(adapterPosition).getObjectId());
                post.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null){

                            data.remove(adapterPosition);
                            //并更新数据
                            recycAdapter.notifyDataSetChanged();
                        }else {
                            Toast.makeText(getActivity(),"删除失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        }
    };

    private void initView() {

        recyclerView = getView().findViewById(R.id.re);

    }
}
